// pages/knows/knows.js
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: ['cjplayVid', 'tfdemoCloud'], //下拉框选项
    pickerValue: 0,
    title:'',   // '什么是垃圾分类',
    iconUrl:'', // '/images/lajifenlei.jpg',
    knowUrl:'', //'k3365dtl2dn',
    //'cloud://cloud1-4gcsxcql7d341258.636c-cloud1-4gcsxcql7d341258-1306724370/数据标注.mp4'
  },
  //获取名称
  titleBlur(e){
    this.data.title = e.detail.value
  },
  //获取图标地址
  iconBlur(e){
    this.data.iconUrl = e.detail.value
  },
  //获取视频地址
  knowBlur(e){
    this.data.knowUrl = e.detail.value
  },
  //通过下拉框hsm
  handlePickerChange(e) {
    this.setData({
      pickerValue: e.detail.value
    });
  },
  // 添加或者修改konwledge数据
  sumit() {
    var that = this
      if (that.data.title == ''){
        wx.showModal({
          title: '温馨提醒',
          content: '请输入知识名称！',
          showCancel: false
        })
      }
      else if (that.data.iconUrl == '') {
        wx.showModal({
          title: '温馨提醒',
          content: '请输入图标地址',
          showCancel: false
        })
      }
      else if (that.data.knowUrl == '') {
        wx.showModal({
          title: '温馨提醒',
          content: '请输入视频地址',
          showCancel: false
        })
      }
      else{
        db.collection('knowledge')
        .where({
          name:that.data.title
        })
        .get()
        .then(res=>{
          console.log(res.data.length)
          if(res.data.length == 0){
            db.collection('knowledge').add({
              data: {
                // block:wx.getStorageSync('block'),
                title:that.data.title,
                iconUrl:that.data.iconUrl,
                hsm:that.data.options[that.data.pickerValue],
                knowUrl:that.data.knowUrl
              }
            })
            .then(res => {
              wx.showModal({
                title: '恭喜',
                content: '知识添加成功！',
                showCancel: false,
                complete() {
                  // 跳转到index页面
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                }
              })
            })

          }else{
            wx.showModal({
              title: '提醒',
              content: '该任务已添加',
              showCancel: false,
            })
          }
        })

      }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})